package com.lut.thirtyparty.controller;

import com.lut.thirtyparty.service.MemberService;
import com.lut.thirtyparty.utils.R;
import com.lut.thirtyparty.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/teacher/msm")
public class MsmConstroller {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private MemberService memberService;

    @GetMapping("/send/{phone}")
    public Boolean sendmsm(@PathVariable String phone){
        // 从redis获取验证码，如果获取到直接返回
        String code=redisTemplate.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code)){
            return true;
        }

        // 生成随机，传递阿里云进行发送
        code= RandomUtil.getFourBitRandom();
        // 调用service发送短信的方法
        Boolean send = memberService.send(phone, code);
        if(send){
            // 发送成功，把发送成功验证码放到redis里面
            // 设置有效shijian
            redisTemplate.opsForValue().set(phone,code,60, TimeUnit.MINUTES);
            return true;
        }else {
            return false;
        }
    }

    
}
